package com.kpmg.datalake.web.controller;


import com.kpmg.datalake.common.cache.CacheService;
import com.kpmg.datalake.common.controller.BaseController;
import com.kpmg.datalake.common.service.IComDictService;
import com.kpmg.datalake.common.utils.ContextHolderUtils;
import com.kpmg.datalake.common.vo.ServerResponse;
import com.kpmg.datalake.common.vo.ViewDictDTO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 字典信息明细表 前端控制器
 * </p>
 *
 * @author azhao6
 * @since 2018-08-16
 */
@RestController
@RequestMapping("/comDict")
public class ComDictController extends BaseController {


    @Resource
    private IComDictService dictService;
  @Resource
  private CacheService cacheService;

    /**
     * 查询数据字典
     *
     * @param viewDictDTO viewDictDTO
     * @return ServerResponse
     */
    @PostMapping("/getViewDictListByCode")
    public ServerResponse<ArrayList<ViewDictDTO>> getViewDictListByCode(@RequestBody ViewDictDTO viewDictDTO) {
        List<ViewDictDTO> dictListByType;
        try {
            String langCd = StringUtils.isBlank(viewDictDTO.getLangCd()) ? ContextHolderUtils.getLocale().toString() : viewDictDTO.getLangCd();
            dictListByType = dictService.getViewDictListByTypeAndParent(viewDictDTO.getDictType(), viewDictDTO.getParentCode(), langCd);
        } catch (Exception e) {
            return super.doError(e);
        }
        return super.doSuccess(new ArrayList<>(dictListByType));

    }

  @GetMapping("/cleanCache")
  public ServerResponse<String> cleanCache() {
    try {
      cacheService.cleanCache();
      return super.doSuccess("清理成功");
    } catch (Exception e) {
      return super.doError(e.getMessage());
    }
  }

}

